#include "main.h"

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

	ExampleGameApp * app = RE_NEW ExampleGameApp; // just object creation. Constructor is not doing anything
	int ret = app->RunEngine(hInstance, lpCmdLine, 640, 480, false, L"Sample Program. Powered by Random Engine.");
	delete app;
	return ret; // this initializes the engine and launches the main loop. When exit, it return the exit code
}

LogicPtr ExampleGameApp::CreateGameAndView()
{
	LogicPtr logic = LogicPtr( RE_NEW BaseGameLogic ); // base game logic will do the trick for now

	logic->VAddView(GameViewPtr(RE_NEW ExampleGameView)); //however, we need to create a RE_NEW HumanView wich can move
	return logic;
}
ExampleGameView::ExampleGameView()
{
	m_inputmap.VSetActionKey(GameAction("MoveForward"), 'W');
	m_inputmap.VSetActionKey(GameAction("MoveBackward"), 'S');
	m_inputmap.VSetActionKey(GameAction("MoveLeft"), 'A');
	m_inputmap.VSetActionKey(GameAction("MoveRight"), 'D');
	//these keys shoud get initial values of false;
	m_inputmap.VKeyUp('W');
	m_inputmap.VKeyUp('A');
	m_inputmap.VKeyUp('S');
	m_inputmap.VKeyUp('D');
	m_id = 1;
}
void ExampleGameView::VOnUpdate(const float fTime)
{
	if(m_inputmap.VIsTrue(GameAction("MoveForward")))
	{
		REE()->VTriggerEvent(EventDataPtr(RE_NEW EventData_View_MoveForward(1.0f, m_id)));//Trigger move forward event
	}
	if(m_inputmap.VIsTrue(GameAction("MoveBackward")))
	{
		REE()->VTriggerEvent(EventDataPtr(RE_NEW EventData_View_MoveForward(-1.0f, m_id)));
	}
	if(m_inputmap.VIsTrue(GameAction("MoveLeft")))
	{
		REE()->VTriggerEvent(EventDataPtr(RE_NEW EventData_View_MoveLeft(1.0f, m_id)));
	}
	if(m_inputmap.VIsTrue(GameAction("MoveRight")))
	{
		REE()->VTriggerEvent(EventDataPtr(RE_NEW EventData_View_MoveLeft(-1.0f, m_id)));
	}
	HumanView::VOnUpdate(fTime);
}